﻿using MvvmCross.Platform;
using MvvmCross.Plugins.Messenger;
using Sidvall.WorkItemManager.UI.Messaging;
using XXX.Workflow;

namespace XXX.UI.ViewModels
{
    public class WorkItem1ContextViewModel : Sidvall.UI.ViewModels.ViewModelBase
    {
        #region Public Members

        #region Context

        private string _Context;
        public string Context
        {
            get
            {
                return _Context;
            }
            set
            {
                if (_Context == value)
                    return;
                _Context = value;
                RaisePropertyChanged("Context");
                OnContextChanged();
            }
        }

        #endregion
        #region ContextItem

        private WorkItem1Context _ContextItem;
        public WorkItem1Context ContextItem
        {
            get
            {
                return _ContextItem;
            }
            set
            {
                if (_ContextItem != null)
                    _ContextItem.PropertyChanged -= OnWorkItemContextPropertyChanged;
                _ContextItem = value;
                if (_ContextItem != null)
                    _ContextItem.PropertyChanged += OnWorkItemContextPropertyChanged;
                RaisePropertyChanged("ContextItem");
                OnContextItemChanged();
            }
        }

        #endregion

        #region OnContextChanged

        protected virtual void OnContextChanged()
        {
            if (this.PublishChange)
            {
                var messenger = Mvx.Resolve<IMvxMessenger>();
                var message = new WorkItemContextMessage(this)
                {
                    Context = this.Context,
                };
                messenger.Publish<WorkItemContextMessage>(message);
            }
        }

        #endregion
        #region OnContextItemChanged

        protected void OnContextItemChanged()
        {
            this.Context = Sidvall.Serialization.SerializerManager.Current.SerializeObject(this.ContextItem);
        }

        #endregion
        #region OnWorkItemContextPropertyChanged

        private void OnWorkItemContextPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            this.Context = Sidvall.Serialization.SerializerManager.Current.SerializeObject(this.ContextItem);
        }

        #endregion

        #region Init

        public void Init(ContextParameter item)
        {
            this.ContextParameter = item;
        }

        #endregion
        #region InitializeContext

        public virtual void InitializeContext()
        {
            this.ContextItem = Sidvall.Serialization.SerializerManager.Current.Deserialize<WorkItem1Context>(this.Context);
        }

        #endregion
        #region Start

        public override void Start()
        {
            var messenger = Mvx.Resolve<IMvxMessenger>();
            _WorkItemContextMessageToken = messenger.Subscribe<WorkItemContextMessage>(OnWorkItemContextMessage);

            if (this.ContextParameter != null)
            {
                this.PublishChange = false;
                this.Context = this.ContextParameter.Context;
                this.PublishChange = true;
                InitializeContext();
                this.ContextParameter = null;
            }
        }

        #endregion

        #endregion
        #region Private Members

        private ContextParameter ContextParameter { get; set; }
        private bool PublishChange { get; set; }

        #region OnWorkItemContextMessage

        private MvxSubscriptionToken _WorkItemContextMessageToken;
        private void OnWorkItemContextMessage(WorkItemContextMessage item)
        {
            if (this.Context == item.Context)
                return;
            this.PublishChange = false;
            this.Context = item.Context;
            this.PublishChange = true;
            InitializeContext();
        }

        #endregion

        #endregion
    }
}
